package work.juzheng;

import java.util.HashSet;
import java.util.Set;

public class Problem73 {

    public void setZeroes(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        HashSet<Integer> setRwo = new HashSet<>();
        Set<Integer> setCol = new HashSet<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == 0) {
                    setRwo.add(i);
                    setCol.add(j);
                }
            }
        }

        for (int i = 0; i < m; i++) {
            if (setRwo.contains(i)) {
                for (int j = 0; j < n; j++) {
                    matrix[i][j] = 0;
                }
            }
        }

        for (int j = 0; j < n; j++) {
            if (setCol.contains(j)) {
                for (int i = 0; i < m; i++) {
                    matrix[i][j] = 0;
                }
            }
        }

    }


    public void setZeroes12(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        boolean rowTag = false;
        boolean colTag = false;
        for (int i = 0; i < m; i++) {
            if (matrix[i][0] == 0) colTag = true;
        }
        for (int j = 0; j < n; j++) {
            if (matrix[0][j] == 0) rowTag = true;
        }

        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (matrix[i][j] == 0) {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }

        for (int i = 1; i < m; i++) {
            if (matrix[i][0] == 0)
                for (int j = 0; j < n; j++) {
                    matrix[i][j] = 0;
                }
        }

        for (int j = 1; j < n; j++) {
            if (matrix[0][j] == 0) {
                for (int i = 0; i < m; i++) {
                    matrix[i][j] =0;
                }
            }
        }

        if (rowTag == true) {
            for (int j = 0; j < n; j++) {
                matrix[0][j] = 0;
            }
        }

        if (colTag == true) {
            for (int i = 0; i < m; i++) {
                matrix[i][0] = 0;

            }
        }
    }

    public void setZeroes1(int[][] matrix) {
        int row = matrix.length;
        int col = matrix[0].length;
        boolean row0_flag = false;
        boolean col0_flag = false;
        // 第一行是否有零
        for (int j = 0; j < col; j++) {
            if (matrix[0][j] == 0) {
                row0_flag = true;
                break;
            }
        }
        // 第一列是否有零
        for (int i = 0; i < row; i++) {
            if (matrix[i][0] == 0) {
                col0_flag = true;
                break;
            }
        }
        // 把第一行第一列作为标志位
        for (int i = 1; i < row; i++) {
            for (int j = 1; j < col; j++) {
                if (matrix[i][j] == 0) {
                    matrix[i][0] = matrix[0][j] = 0;
                }
            }
        }
        // 置0
        for (int i = 1; i < row; i++) {
            for (int j = 1; j < col; j++) {
                if (matrix[i][0] == 0 || matrix[0][j] == 0) {
                    matrix[i][j] = 0;
                }
            }
        }
        if (row0_flag) {
            for (int j = 0; j < col; j++) {
                matrix[0][j] = 0;
            }
        }
        if (col0_flag) {
            for (int i = 0; i < row; i++) {
                matrix[i][0] = 0;
            }
        }
    }

    public static void main(String[] args) {
        Problem73 problem73 = new Problem73();
        int[][] nums = new int[][]{{1, 0, 3}};
        problem73.setZeroes12(nums);
    }


}
